{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "4389eb89-a5e0-424a-be99-cf250cf9efb1",
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "ece780f7-b59e-48f8-b1eb-74a576d5bdcb",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'2.3.3'"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.__version__"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "77d564ad-36c2-48ad-94fd-04bf53ba9d0f",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[0, 1, 4, 9, 16, 25, 36, 49, 64, 81]\n"
     ]
    }
   ],
   "source": [
    "a = [i**2 for i in range(10)]\n",
    "print(a)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "1fb7a661",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[  0   2   8  18  32  50  72  98 128 162]]\n"
     ]
    }
   ],
   "source": [
    "b = np.arange(10)**2\n",
    "c = np.array(a)\n",
    "d = (b+c).reshape(1,10)\n",
    "print(d)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "25ef065c",
   "metadata": {},
   "source": [
    "# numpy的核心array对象以及创建array的方法"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "6f2b63bd",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(2, 5)\n",
      "2\n",
      "10\n",
      "int64\n",
      "8\n"
     ]
    }
   ],
   "source": [
    "d = d.reshape(2,5)\n",
    "print(d.shape)\n",
    "print(d.ndim) #返回array的维度数\n",
    "print(d.size) #返回array的元素个数\n",
    "print(d.dtype) #返回array中元素的数据类型\n",
    "print(d.itemsize) #返回array中每个元素的字节大小"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "821e7f91",
   "metadata": {},
   "source": [
    "创建array的方法："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "32517abb",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.arange(10)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "2ed30a29",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([1, 3, 5, 7, 9])"
      ]
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.arange(1,10,2) #起始，终止，步长"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "id": "97b2a304",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([1., 1., 1., 1., 1., 1., 1., 1., 1., 1.])"
      ]
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.ones(10)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "id": "acd0c3c6",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1., 1., 1., 1.],\n",
       "       [1., 1., 1., 1.],\n",
       "       [1., 1., 1., 1.]])"
      ]
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.ones((3,4)) #创建一个3行4列的全1数组"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "id": "c8873a04",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1, 1, 1, 1, 1],\n",
       "       [1, 1, 1, 1, 1]])"
      ]
     },
     "execution_count": 28,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.ones_like(d) #创建一个和d形状相同的全1数组"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "id": "4cc14e0b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[0., 0., 0., 0.],\n",
       "       [0., 0., 0., 0.],\n",
       "       [0., 0., 0., 0.]])"
      ]
     },
     "execution_count": 29,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.zeros((3,4)) #创建一个3行4列的全0数组"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "id": "27fbf4ed",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[0, 0, 0, 0, 0],\n",
       "       [0, 0, 0, 0, 0]])"
      ]
     },
     "execution_count": 30,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.zeros_like(d) #创建一个和d形状相同的全0数组"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "id": "fc2e89b0",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([1., 1., 1., 1., 1., 1., 1., 1., 1., 1.])"
      ]
     },
     "execution_count": 31,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.empty(10) #创建一个未初始化的数组，数组中的值是随机的"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9b9b6034",
   "metadata": {},
   "source": [
    "使用full创建指定值的数组\n",
    "np.full(shape,full_value,dtype=None,order='C)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "id": "a7e36fea",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[7, 7, 7],\n",
       "       [7, 7, 7]])"
      ]
     },
     "execution_count": 32,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.full((2,3),7) #创建一个2行3列的数组，所有元素都为7"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "id": "441f7cd4",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[9, 9, 9, 9, 9],\n",
       "       [9, 9, 9, 9, 9]])"
      ]
     },
     "execution_count": 33,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.full_like(d,9) #创建一个和d形状相同的数组，所有元素都为9"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8396debc",
   "metadata": {},
   "source": [
    "使用random模块生成随机数的数组\n",
    "random(d0,d1,...,dn)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "id": "7d308bc3",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "-0.3970052327643103"
      ]
     },
     "execution_count": 34,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.random.randn()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "id": "5e29f7c1",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[-0.36869898, -1.36524815,  0.14865933],\n",
       "       [-0.81049455,  2.81741425, -0.12756855]])"
      ]
     },
     "execution_count": 35,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.random.randn(2,3) #创建一个2行3列的数组，数组中的值服从标准正态分布"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9eba23e9",
   "metadata": {},
   "source": [
    "# Array本身支持大量的操作函数\n",
    "这些操作如果用python实现需要很多for循环，用numpy数组很容易实现"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "id": "414f91a0",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[0, 1, 2, 3, 4],\n",
       "       [5, 6, 7, 8, 9]])"
      ]
     },
     "execution_count": 37,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "A = np.arange(10).reshape(2,5)\n",
    "A"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f0d066e8",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(2, 5)"
      ]
     },
     "execution_count": 38,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "A.shape #返回数组的形状"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "8356d15b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 1,  2,  3,  4,  5],\n",
       "       [ 6,  7,  8,  9, 10]])"
      ]
     },
     "execution_count": 39,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "A+1 #数组的每个元素都加1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f38c06c1",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 0,  3,  6,  9, 12],\n",
       "       [15, 18, 21, 24, 27]])"
      ]
     },
     "execution_count": 40,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "A*3 #数组的每个元素都乘以3"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "test_env",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
